﻿using ECBS_Backend.model;
using ECBS_Backend.service;
using ECBS_Backend.dao;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using ECBS_Backend.utils;
using Microsoft.AspNetCore.Authorization;

namespace ECBS_Backend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EnterpriseController : ControllerBase
    {

        private readonly AdminService adminService;
        private readonly EnrollmentService enrollmentService;
        private readonly GradeService gradeService;

        public EnterpriseController(AdminService adminService, EnrollmentService enrollmentService, GradeService gradeService)
        {
            this.adminService = adminService;
            this.enrollmentService = enrollmentService;
            this.gradeService = gradeService;
        }

        /// <summary>
        /// 公司登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        [HttpGet("login")]
        public ActionResult<string> Login([FromQuery]string userName, [FromQuery]string password)
        {
            try{
                var s = adminService.EnterpriseLogin(userName, password);
                string token = JwtGenerator.GenerateJWT("Enterprise", userName);

                WeIDToken weIDToken = new WeIDToken { WeID = s.WeID, Token = token };

                return Ok(new { message = "登陆成功", data = weIDToken, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

          
        /// <summary>
        /// 核验学籍证书
        /// </summary>
        [HttpPost("verifyEnrollment")]
        public ActionResult<bool> VerifyEnrollment([FromForm]IFormFile file)
        {
            try
            {
                bool isTure = enrollmentService.VertifyEnrollment(file);
                Response.Headers.Add("Access-Control-Allow-Origin", "*");
                Response.Headers.Add("Access-Control-Allow-Methods", "POST");
                Response.Headers.Add("Access-Control-Request-Headers", "*");
                Response.Headers.Add("Access-Control-Expose-Headers", "*");
                Response.Headers.Add("Access-Control-Max-Age", "86400");
                return Ok(new { message = "核验请求发送成功", data = isTure, code = 200 });
            }
            catch(Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 核验成绩证书
        /// </summary>
        [HttpPost("verifyGrade")]
        public ActionResult<bool> VerifyGrade(IFormFile file)
        {
            try
            {
                bool isTure = gradeService.VertifyGrade(file);
                Response.Headers.Add("Access-Control-Allow-Origin", "*");
                Response.Headers.Add("Access-Control-Allow-Methods", "POST");
                Response.Headers.Add("Access-Control-Request-Headers", "*");
                Response.Headers.Add("Access-Control-Expose-Headers", "*");
                Response.Headers.Add("Access-Control-Max-Age", "86400");
                return Ok(new { message = "核验请求发送成功", data = isTure, code = 200 });
            }
            catch (Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
            
        }
    }
}
